<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- worldtimeclockplugin.qdoc -->
  <title>World Time Clock Plugin Example | Qt Designer Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.14</a></td><td ><a href="qtdesigner-manual.html">Qt Designer Manual</a></td><td >World Time Clock Plugin Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtdesigner-manual.html">Qt 5.14.2 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#worldtimeclock-class">WorldTimeClock Class</a></li>
<li class="level1"><a href="#worldtimeclockplugin-class">WorldTimeClockPlugin Class</a></li>
<li class="level1"><a href="#the-project-file-worldtimeclockplugin-pro">The Project File: worldtimeclockplugin.pro</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">World Time Clock Plugin Example</h1>
<span class="subtitle"></span>
<!-- $$$worldtimeclockplugin-brief -->
<p>Creating a custom widget plugin for <i>Qt Designer</i> that uses signals and slots.</p>
<!-- @@@worldtimeclockplugin -->
<!-- $$$worldtimeclockplugin-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/worldtimeclockplugin-example.png" alt="" /></p><p>In this example, we simply extend the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example and its custom widget (based on the <a href="../qtwidgets/qtwidgets-widgets-analogclock-example.html">Analog Clock</a> example), by introducing the concept of signals and slots.</p>
<p>The World Time Clock Plugin example consists of two classes:</p>
<ul>
<li><code>WorldTimeClock</code> is a custom clock widget with hour and minute hands that is automatically updated every few seconds.</li>
<li><code>WorldTimeClockPlugin</code> exposes the <code>WorldTimeClock</code> class to <i>Qt Designer</i>.</li>
</ul>
<p>First we will take a look at the <code>WorldTimeClock</code> class which extends the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example's <code>AnalogClock</code> class by providing a signal and a slot. Then we will take a quick look at the <code>WorldTimeClockPlugin</code> class, but this class is in most parts identical to the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example's implementation.</p>
<p>Finally we take a look at the plugin's project file. The project file for custom widget plugins needs some additional information to ensure that they will work within <i>Qt Designer</i>. This is also covered in the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example, but due to its importance (custom widget plugins rely on components supplied with <i>Qt Designer</i> which must be specified in the project file that we use) we will repeat it here.</p>
<a name="worldtimeclock-class"></a>
<h4 id="worldtimeclock-class">WorldTimeClock Class</h4>
<p>The <code>WorldTimeClock</code> class inherits <a href="../qtwidgets/qwidget.html">QWidget</a>, and is a custom clock widget with hour and minute hands that is automatically updated every few seconds. What makes this example different from the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example, is the introduction of the signal and slot in the custom widget class:</p>
<pre class="cpp">

  <span class="keyword">class</span> QDESIGNER_WIDGET_EXPORT WorldTimeClock : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span>
  {
      Q_OBJECT

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> WorldTimeClock(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

  <span class="keyword">public</span> <span class="keyword">slots</span>:
      <span class="type">void</span> setTimeZone(<span class="type">int</span> hourOffset);

  <span class="keyword">signals</span>:
      <span class="type">void</span> updated(<span class="type"><a href="../qtcore/qtime.html">QTime</a></span> currentTime);

  <span class="keyword">protected</span>:
      <span class="type">void</span> paintEvent(<span class="type">QPaintEvent</span> <span class="operator">*</span>event) override;

  <span class="keyword">private</span>:
      <span class="type">int</span> timeZoneOffset <span class="operator">=</span> <span class="number">0</span>;
  };

</pre>
<p>Note the use of the <a href="qdesignercustomwidgetinterface.html#QDESIGNER_WIDGET_EXPORT">QDESIGNER_WIDGET_EXPORT</a> macro. This is needed to ensure that <i>Qt Designer</i> can create instances of the widget on some platforms, but it is a good idea to use it on all platforms.</p>
<p>We declare the <code>setTimeZone()</code> slot with an associated <code>timeZoneOffset</code> variable, and we declare an <code>updated()</code> signal which takes the current time as argument and is emitted whenever the widget is repainted.</p>
<p class="centerAlign"><img src="images/worldtimeclock-connection.png" alt="" /></p><p>In <i>Qt Designer</i>'s workspace we can then, for example, connect the <code>WorldTimeClock</code> widget's <code>updated()</code> signal to a <a href="../qtwidgets/qtimeedit.html">QTimeEdit</a>'s <a href="../qtwidgets/qdatetimeedit.html#time-prop">setTime()</a> slot using <i>Qt Designer</i>'s mode for editing signal and slots.</p>
<p class="centerAlign"><img src="images/worldtimeclock-signalandslot.png" alt="" /></p><p>We can also connect a <a href="../qtwidgets/qspinbox.html">QSpinBox</a>'s <a href="../qtwidgets/qspinbox.html#valueChanged">valueChanged()</a> signal to the <code>WorldTimeClock</code>'s <code>setTimeZone()</code> slot.</p>
<a name="worldtimeclockplugin-class"></a>
<h4 id="worldtimeclockplugin-class">WorldTimeClockPlugin Class</h4>
<p>The <code>WorldTimeClockPlugin</code> class exposes the <code>WorldTimeClock</code> class to <i>Qt Designer</i>. Its definition is equivalent to the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example's plugin class which is explained in detail. The only part of the class definition that is specific to this particular custom widget is the class name:</p>
<pre class="cpp">

  <span class="keyword">class</span> WorldTimeClockPlugin : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">,</span>
                               <span class="keyword">public</span> <span class="type"><a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a></span>
  {
      Q_OBJECT
      Q_PLUGIN_METADATA(IID <span class="string">&quot;org.qt-project.Qt.QDesignerCustomWidgetInterface&quot;</span>)
      Q_INTERFACES(<span class="type"><a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a></span>)

  <span class="keyword">public</span>:
      <span class="keyword">explicit</span> WorldTimeClockPlugin(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> nullptr);

      bool isContainer() <span class="keyword">const</span> override;
      bool isInitialized() <span class="keyword">const</span> override;
      <span class="type">QIcon</span> icon() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> domXml() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> group() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> includeFile() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> name() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> toolTip() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> whatsThis() <span class="keyword">const</span> override;
      <span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>createWidget(<span class="type"><a href="../qtwidgets/qwidget.html">QWidget</a></span> <span class="operator">*</span>parent) override;
      <span class="type">void</span> initialize(<span class="type"><a href="qdesignerformeditorinterface.html">QDesignerFormEditorInterface</a></span> <span class="operator">*</span>core) override;

  <span class="keyword">private</span>:
      bool initialized <span class="operator">=</span> <span class="keyword">false</span>;
  };

</pre>
<p>The plugin class provides <i>Qt Designer</i> with basic information about our plugin, such as its class name and its include file. Furthermore it knows how to create instances of the <code>WorldTimeClockPlugin</code> widget. <code>WorldTimeClockPlugin</code> also defines the <a href="qdesignercustomwidgetinterface.html#initialize">initialize()</a> function which is called after the plugin is loaded into <i>Qt Designer</i>. The function's <a href="qdesignerformeditorinterface.html">QDesignerFormEditorInterface</a> parameter provides the plugin with a gateway to all of <i>Qt Designer</i>'s API's.</p>
<p>The <code>WorldTimeClockPlugin</code> class inherits from both <a href="../qtcore/qobject.html">QObject</a> and <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a>. It is important to remember, when using multiple inheritance, to ensure that all the interfaces (i.e&#x2e; the classes that doesn't inherit <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a>) are made known to the meta object system using the <a href="../qtcore/qobject.html#Q_INTERFACES">Q_INTERFACES</a>() macro. This enables <i>Qt Designer</i> to use <a href="../qtcore/qobject.html#qobject_cast">qobject_cast</a>(QObject * object) to query for supported interfaces using nothing but a <a href="../qtcore/qobject.html">QObject</a> pointer.</p>
<p>The implementation of the <code>WorldTimeClockPlugin</code> is also equivalent to the plugin interface implementation in the <a href="qtdesigner-customwidgetplugin-example.html">Custom Widget Plugin</a> example (only the class name and the implementation of <a href="qdesignercustomwidgetinterface.html#domXml">QDesignerCustomWidgetInterface::domXml</a>() differ). The main thing to remember is to use the <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro to export the <code>WorldTimeClockPlugin</code> class for use with <i>Qt Designer</i>:</p>
<pre class="cpp">

      Q_PLUGIN_METADATA(IID <span class="string">&quot;org.qt-project.Qt.QDesignerCustomWidgetInterface&quot;</span>)

</pre>
<p>Without this macro, there is no way for Qt Designer to use the widget.</p>
<a name="the-project-file-worldtimeclockplugin-pro"></a>
<h4 id="the-project-file-worldtimeclockplugin-pro">The Project File: worldtimeclockplugin.pro</h4>
<p>The project file for custom widget plugins needs some additional information to ensure that they will work as expected within <i>Qt Designer</i>:</p>
<pre class="cpp">

  QT          += widgets uiplugin
  CONFIG     += plugin
  TEMPLATE    = lib

</pre>
<p>The <code>TEMPLATE</code> variable's value in conjunction with the keyword <code>plugin</code> in the <code>CONFIG</code> variable make <code>qmake</code> create the custom widget as a plugin library.</p>
<p>The <code>QT</code> variable contains the keyword <code>uiplugin</code>. This plugin type provides a factory function for custom widget creation by implementing the abstract interfaces <a href="qdesignercustomwidgetinterface.html">QDesignerCustomWidgetInterface</a> or <a href="qdesignercustomwidgetcollectioninterface.html">QDesignerCustomWidgetCollectionInterface</a>, suitable for use with <a href="../qtuitools/quiloader.html">QUiLoader</a>. It does not have a dependency on the <i>Qt Designer</i> libraries. Plugins accessing other interfaces of <i>Qt Designer</i> to implement container extensions or other <i>Qt Designer</i> specific functionality follow different rules and are covered by other examples.</p>
<p>The header and source files for the widget are declared in the usual way, and in addition we provide an implementation of the plugin interface so that <i>Qt Designer</i> can use the custom widget.</p>
<pre class="cpp">

  HEADERS     = worldtimeclock.h \
                worldtimeclockplugin.h
  SOURCES     = worldtimeclock.cpp \
                worldtimeclockplugin.cpp

</pre>
<p>It is important to ensure that the plugin is installed in a location that is searched by <i>Qt Designer</i>. We do this by specifying a target path for the project and adding it to the list of items to install:</p>
<pre class="cpp">

  target.path = $$[QT_INSTALL_PLUGINS]/designer
  INSTALLS += target

</pre>
<p>The custom widget is created as a library, and will be installed alongside the other <i>Qt Designer</i> plugins when the project is installed (using <code>make install</code> or an equivalent installation procedure). Later, we will ensure that it is recognized as a plugin by <i>Qt Designer</i> by using the <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a>() macro to export the relevant widget information.</p>
<p>Note that if you want the plugins to appear in a Visual Studio integration, the plugins must be built in release mode and their libraries must be copied into the plugin directory in the install path of the integration (for an example, see <code>C:/program files/trolltech as/visual studio integration/plugins</code>).</p>
<p>For more information about plugins, see the <a href="../qtdoc/plugins-howto.html">How to Create Qt Plugins</a> document.</p>
<p><a href="https://code.qt.io/cgit/qt/qttools.git/tree/examples/designer/worldtimeclockplugin?h=5.14">Example project @ code.qt.io</a></p>
</div>
<!-- @@@worldtimeclockplugin -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
